
// 网关配置的前缀
let account_server = "/accountSer"

let Authorization = "";
let refreshToken = "";

let authTree = [];
let authChecked = [];

let role_id = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        account_server = data.accountServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")

    getRoleList();
}

function getAuthTree(){
    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "authorityManagement");

    $.ajax({
        type: "get",
        async: false,
        url: account_server + "/account/auth/tree",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("权限树");
            console.log(data.datas);
            authTree = data.datas;
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    });
}

function getAuthChecked(roleId){
    $.ajax({
        type: "get",
        async: false,
        url: account_server + "/account/auth/checked?roleId="+roleId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("拥有权限");
            console.log(data.datas)
            authChecked = data.datas;
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    });
}

// 初始化树
function initTree(roleId){
    getAuthChecked(roleId);
    getAuthTree();
    leg.tree({
        ele:".tree",//选者
        data:authTree,//数据
        arrs:authChecked,//选中的id
        cascade:true,//级联
    });
}

function getRoleList(){
    $.ajax({
        type: "get",
        async: false,
        url: account_server + "/account/auth/getRoleList",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("角色列表");

            unauthorized(data);

            $.each(data.datas, function (i, item){
                let selected = false;
                let classInfo = "nav-link";
                if (i === 0){
                    initTree(item.id);
                    role_id = item.id;
                    selected = true;
                    classInfo = "nav-link active";
                }
                $("#roleList").append(
                    "<li class='nav-item' role='presentation'>"+
                    "<a class='"+classInfo+"' onClick='changeRole(\""+item.id+"\")' data-toggle='pill'"+
                    "role='tab' aria-selected='"+selected+"'>"+item.name+"</a>"+
                    "</li>"
                )

            })
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    });
}

function changeRole(roleId){
    role_id = roleId;
    initTree(roleId);
    initTree(roleId);
}

$("#save").on("click", () => {
    let roleList = leg.getCheckedNodes();
    $.ajax({
        type: "POST",
        async: false,
        url: account_server + "/account/auth/updateAuth",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            roleId: role_id,
            roleList: JSON.stringify(roleList),
        },
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg);

                // 获取令牌
                Authorization = getCookie("Authorization")
                refreshToken = getCookie("refreshToken")
                refreshAuth(Authorization, refreshToken, "");
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg);
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    });
})
